package com.example.lcl.exp2019_3;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.content.ContextCompat;
import android.view.MotionEvent;
import android.view.View;

public class CustomView extends View {
    //画脸、鼻子、弧形的画笔和颜色
    Paint paint1;
    int color1;
    //画嘴的画笔和颜色
    Paint paint2;
    int color2;
    //画眼睛的画笔和颜色
    Paint paint3;
    int color3;

    //组合图形
    Path path1;

    //中心点
    PointF point = new PointF(550,450);

    //如果不用在布局文件中定义View,则实现此构造函数就足够了---在此处准备好绘图使用的所有对象和数据
    public CustomView(Context context) {
        super(context);

        //设置白底
        setBackgroundColor(Color.WHITE);

        //初始化画笔和颜色;
        color1=Color.BLUE;
        paint1=new Paint();
        paint1.setStyle(Paint.Style.STROKE);
        paint1.setStrokeWidth(15);    //实芯画笔的宽度无效
        paint1.setColor(color1);
        paint1.setAlpha(255);

        //初始化画笔和颜色2
        color2=Color.RED;
        paint2=new Paint();
        paint2.setStyle(Paint.Style.FILL);
        paint2.setColor(color2);
        paint2.setAlpha(255);

        //初始化画笔和颜色3
        color3 = Color.BLACK;
        paint3 = new Paint();
        paint3.setStyle(Paint.Style.FILL);
        paint3.setColor(color3);
        paint3.setAlpha(255);

        //三角形的组合图形


    }

    //所有的绘图操作均在此进行
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //画出矩形
        canvas.drawRect(point.x-300, point.y-350, point.x+300, point.y+350, paint1); //左上角坐标，右下角坐标
        canvas.drawRect(point.x-200,point.y+150,point.x+200,point.y+250,paint2);
        //画出三角形
        path1 = new Path();
        path1.moveTo(point.x,point.y-100);
        path1.lineTo(point.x-100,point.y+50);
        path1.lineTo(point.x+100,point.y+50);
        path1.lineTo(point.x,point.y-100);
        canvas.drawPath(path1, paint1);
        //画出弧形，第一个所需参数矩形区域
        RectF rec=new RectF();
        rec.set(point.x-200, point.y-120, point.x+200, point.y+130);
        canvas.drawArc(rec,35,110,false,paint1);
        //画圆形
        canvas.drawCircle(point.x-150,point.y-200,70,paint3);
        //画椭圆
        RectF rec1=new RectF();
        rec1.set(point.x+80, point.y-230, point.x+220, point.y-170);
        canvas.drawOval(rec1,paint3);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event){
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            point.set(event.getX(), event.getY());
            paint1.setAlpha(100);
            paint2.setAlpha(100);
            paint3.setAlpha(100);
            invalidate();
        }
        if (event.getAction() == MotionEvent.ACTION_UP) {
            paint1.setAlpha(255);
            paint2.setAlpha(255);
            paint3.setAlpha(255);
            invalidate();
        }
        return true;
    }
}
